package com.tan.lbs;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.tan.lbs.dao.GameDAO;
import com.tan.lbs.dao.impl.GameDAOImpl;
import com.tan.lbs.entity.GameHeader;
import com.tan.lbs.entity.MobileUser;
import com.tan.lbs.util.Constants;
import com.tan.lbs.util.JSONConnector;
import com.tan.lbs.util.SQLiteDatabaseHelper;
import com.tan.lbs.util.URLs;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
	/** Called when the activity is first created. */
	private Button loginBtn;
	private Button signupBtn;
	private EditText usernameTxt;
	private EditText passwordTxt;
	private TextView loginMessage;
	private SharedPreferences settings;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		settings = getSharedPreferences(Constants.PREFS_NAME, 0);
		loginBtn = (Button) findViewById(R.id.loginBtn);
		loginBtn.setOnClickListener(new MainCoverClickListener());
		usernameTxt = (EditText) findViewById(R.id.loginUsername);
		passwordTxt = (EditText) findViewById(R.id.loginPassword);
		usernameTxt.setText(settings.getString("username", ""));
		passwordTxt.setText(settings.getString("password", ""));
		loginMessage = (TextView) findViewById(R.id.loginMessage);
		signupBtn = (Button) findViewById(R.id.signupBtn);
		signupBtn.setOnClickListener(new MainCoverClickListener());

		
	}
	
	



	class MainCoverClickListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			if (view.equals(loginBtn)) {
				try {
					
					List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
					nameValuePairs.add(new BasicNameValuePair("username", usernameTxt.getText().toString()));
					nameValuePairs.add(new BasicNameValuePair("password", passwordTxt.getText().toString()));
					loginMessage.setText("Please wait...");
					JSONObject returnJSON = JSONConnector.connectByPost(
							URLs.loginURL,"login", nameValuePairs);

					String message = returnJSON.getString("message");
					String userID = returnJSON.getString("userID");
					if(message.equals("true")) {
						SharedPreferences.Editor editor = settings.edit();
						editor.putString("username", usernameTxt.getText().toString());
						editor.putString("password", passwordTxt.getText().toString());
						editor.putString("userID", userID);
						editor.commit();
						Intent intent = new Intent();
						intent.setClass(MainActivity.this, GamesActivity.class);
						MainActivity.this.startActivity(intent);
					} else if(message.equals("false")){
						loginMessage.setText("Invalid username or password");
					} else {
						loginMessage.setText(message);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			if (view.equals(signupBtn)) {
				MainActivity.this.deleteDatabase(Constants.LOCAL_DB_NAME);
			}

		}

	}
}